//
//bib_menu.c
//
//

#include "project.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void display_menu()
{   
    printf("\n\n\t\t=HASH-BIBMANAGER (Efficient Ed.)=\n");    
    printf("\n");    
    printf("[1]\t\tExit\n");
    printf("[2]\t\tInsert\n");
    printf("[3]\t\tDisplay All\n");
    printf("[4]\t\tDisplay a Publication\n");
    printf("[5]\t\tModify a Publication\n");
    printf("[6]\t\tDelete a Publication\n");
    printf("[7]\t\tExport to File\n");
    printf("[8]\t\tImport from File\n");
    printf("[9]\t\tDisplay Sorted Publications (by year)\n\n");
    printf("\t\t\tEnter an option: ");
}

const char * get_doi(){
    int memory_read;
    int nbytes;
    const char *doi;
    puts("Enter DOI of publication:");
    doi = (char *)malloc(nbytes+1);
    memory_read=getline(&doi, &nbytes, stdin);
    return doi;
}

void display_pub(entry_t **hash_table, const char *doi){
    publication_t *current;
    current = get_entry(hash_table, doi);
    
    if (current != NULL) {
        printf("\tDOI:\t%-s\n", current->doi);
        
        if (current->isJournal == 0) {
            printf("\tConference Venue:\t%-s\n", current->conference.venue);
        } else {
            printf("\tJournal Volume, Article Number:\t%-i, %i\n", current->journal.volume, current->journal.article);
        }
        
        printf("\tTitle:\t%-s\n", current->title);
        printf("\tYear:\t%-d\n", current->year);        
        printf("\tISBN:\t%-s\n", current->isbn);
        printf("\tSource:\t%-s\n", current->source);
        printf("\tAuthors:\t%-s\n", current->authors);
        printf("\tImpact:\t%-f\n", current->impact);
    
    } else {
        printf("Could not find matching Publication to display.\n");
    }
}

void display_pubs(entry_t **hash_table)
{
    int i;
    for ( i = 0; i < TABLE_SIZE; i++) {
        if (hash_table[i] != NULL) {
            entry_t *current;
            for(current = hash_table[i]; current != NULL; current=current->next){
                display_pub_summary(current->publication);
                
            }
        }
    }
}

void modify_pub(entry_t **hash_table, const char *doi) {
    publication_t *pub = get_entry(hash_table, doi);
    display_pub_summary(pub);
    printf("What would you like to modify?\n");
    printf("\t(1) Title\n\t(2) ISBN\n\t(3) Source\n\t(4) Authors\n\t(5) Year\n\t(6) Impact\n");
    
    int input = atoi(read_text());
    char *new;
    
    switch (input) {
        case 1:
            printf("\tNew Title: ");
            new = read_text();
            free(pub->title);
            pub->title = strdup(new);
            
            break;
            
        case 2:
            printf("\tNew ISBN: ");
            new = read_text();
            free(pub->isbn);
            pub->isbn = strdup(new);
            
            break;
            
        case 3:
            printf("\tNew Source: ");
            new = read_text();
            free(pub->source);
            pub->source = strdup(new);
            
            break;
            
        case 4:
            printf("\tNew Author: ");
            new = read_text();
            free(pub->authors);
            pub->authors = strdup(new);
            
            break;
            
        case 5:
            printf("\tNew Year: ");
            new = read_text();
            pub->year = atoi(new);
            
            break;
            
        case 6:
            printf("\tNew Impact: ");
            new = read_text();
            pub->impact= atoi(new);
            
            break;

        default:
            break;
    }    
}

void delete_selected_pub(entry_t **hash_table, const char *doi)
{
    publication_t *current;
    current = get_entry(hash_table, doi);
    printf("removing publication: %p\n", current);
    
    if (current != NULL) 
    {
        
        if (current->isJournal == 0) 
        {
            free(current->conference.venue);
        } 
        else 
        {
            free(current->journal.volume);
            free(current->journal.article);
        }
        
        free(current->doi);
        
        free(current->title);
        
        free(current->isbn);
        
        free(current->source);
        
        free(current->authors);
	
        free(current);
    } 
    else 
    {
        printf("Could not find matching Publication to remove.\n");
    }
}

/*void display_sorted_pubs(entry_t **array)
 
 {
 
 int i, j;
 
 publication_t *tmp;
 
 
 
 // go through the array from 1 to nmemb-1
 
 // here, i is an offset
 
 for (i = 1; i < SIZE; i++)
 
 {
 
 // store element of i position in temp
 
 tmp = array[i];
 
 
 
 // compare all the elements from
 
 // j=i-1 to 0 with the value in tmp
 
 j = i - 1;
 
 while (tmp->year[0] < array[j]->year[0] && j >= 0)
 
 {
 
 // move the hole to the j position
 
 array[j + 1] = array[j];
 
 j--;
 
 }
 
 array[j + 1] = tmp;
 
 }
 
 }
 
 */







/*long int data_read()
 
 {
 
 char *ch = NULL;
 
 size_t len = 0;
 
 int line = getline(&ch, &len, stdin);
 
 char *ptr;
 
 
 
 //printf("my_string is %s dabe.\n",ch);
 
 ch[strlen(ch)-1]='\0';
 
 //printf("my_string is %s dabe. \n",ch);
 
 int in = strtol(ch, &ptr, 10);
 
 //printf("Returned %i",in);
 
 free((void*) ch);
 
 return in;
 
 }*/




